package com.nbot.server.job;

        import cn.hutool.log.StaticLog;
        import com.nbot.common.command.s2b.ReportCommand;
        import com.nbot.server.BotServer;
        import com.nbot.server.domain.AlarmDO;
        import com.nbot.server.domain.AnalogalarmDO;
        import com.nbot.server.domain.ChecklogDO;
        import com.nbot.server.service.AlarmService;
        import com.nbot.server.service.AnalogalarmService;
        import com.nbot.server.service.ChecklogService;
        import com.nbot.server.util.SpringUtil;
        import com.nbot.server.workers.OpcWorker;
        import org.jinterop.dcom.common.JIException;
        import org.quartz.Job;
        import org.quartz.JobExecutionContext;
        import org.quartz.JobExecutionException;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Component;

        import java.io.UnsupportedEncodingException;
        import java.net.UnknownHostException;
        import java.util.Date;
        import java.util.HashMap;
        import java.util.List;
        import java.util.Map;

/**
 * 3厂mysql报警监控任务
 */
@Component
public class MysqlAlarm3ChangJob implements Job {


    @Override
    public void execute(JobExecutionContext arg0) throws JobExecutionException {
        StaticLog.info("3厂水井超欠注监控 * 4厂关联报警");
        OpcWorker opcWorker = (OpcWorker) SpringUtil.getBean("opcWorker");
        try {
//            opcWorker.checkWaterWell();//-3厂
            opcWorker.checkRelevanceAlarm();//-4厂
        } catch (JIException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

}
//@Component
//public class MysqlAlarm3ChangJob implements Job {
//    @Autowired
//    private AlarmService alarmService;
////    @Autowired
////    private BotServer botServer;
//    private static int DB_COUNT = 0;
//
//    @Autowired
//    private ChecklogService checklogService;
//    @Autowired //add wyzc 180606
//    private AnalogalarmService analogalarmService;
//
//    /**
//     * 设置DB_COUNT的值，同步方法
//     *
//     * @param count
//     */
//    private static void setCount(int count) {
//        synchronized (MysqlAlarm3ChangJob.class) {
//            DB_COUNT = count;
//        }
//    }
//
//    @Override
//    public void execute(JobExecutionContext arg0) throws JobExecutionException {
//        StaticLog.info("执行3厂mysql报警监控任务");
//
//        //第一次运行时
//        if (DB_COUNT == 0) {
//            setCount(alarmService.count(null) - 1);
//        }
//        //获取当前总数
//        int currentCount = alarmService.count(null);
//        // 大于时表示有新报警
//        if (currentCount > DB_COUNT) {
//
//            int discar = currentCount - DB_COUNT;
//            setCount(currentCount);
//            Map<String, Object> params = new HashMap<>();
//            params.put("sort", "AlarmTime");
//            params.put("order", "desc");
//            params.put("offset", 0);
//            params.put("limit", discar);
//            List<AlarmDO> list = alarmService.list(params);
//
//            for (AlarmDO alarmDO : list) {
//                AnalogalarmDO analogpoints = null;
//                analogpoints = analogalarmService.getopcdatapath(alarmDO.getTagname());
//                if (analogpoints != null) {
//                    ReportCommand reportCommand = new ReportCommand();
//                    //--------代码段开始标志-------- 代码段功能：请根据实际情况从alarmDO获取需要的信息组成reportCommand
//                    reportCommand.setValueNow(alarmDO.getAlarmvalue());
//                    reportCommand.setReportMsg(alarmDO.getTagcomment());
//                    reportCommand.setWarnLevel("1");// 1-代表生产  权宜之计 wyzc
//                    reportCommand.setPriority(2);
//                    //--------代码段结束标志--------
////                    BotServer.putCommand(reportCommand);
//                    try {
//                        BotServer.addUartMsg(reportCommand);
//                    } catch (UnsupportedEncodingException e) {
//                        e.printStackTrace();
//                    }
//                    System.err.println("有了！！");
//                    ChecklogDO checklog = new ChecklogDO();
//                    checklog.setTag(alarmDO.getTagname());
//                    checklog.setDispdesc(((ReportCommand) reportCommand).getReportMsg());
//                    checklog.setChecktimestamp(alarmDO.getAlarmtime());
//                    checklog.setCheckvalue(alarmDO.getAlarmtype());
//                    checklog.setTruthvalue(alarmDO.getAlarmvalue());
//                    checklog.setAlarmlevel(reportCommand.getWarnLevel());
//                    checklogService.insert(checklog);
//                }
//            }
//        }
//    }
//
//}